<template>
	<div class="tikuBg" :class="darkmode ? 'darkmode' : ''">
		
		
		<u-sticky>
			<div class="banner">
				<image src="https://www.baixuemianshi.com/zhuanti/images/swiper_xcx2.jpg" mode="widthFix"></image>
			</div>
			<div class="bjTlt">{{zutiTitle}}</div>
		</u-sticky>
		
		<div class="container">
			<div class="items">
				<template v-if="banjiDetail.type=='刷题模式'">
					<div class="item" v-for="(item,index) in timuDetail" :key="index">
						<h3>
							<u-parse :content="(index + 1) + '、' + item.title" :showImgMenu="false"></u-parse>
						</h3>
						<div class="d_btns">
							
							<div class="time">{{item.time}}</div>
							<div class="btn" v-if="userinfo.limit == '老师'">
								<u-button type="error"  v-if="item.status=='silu'" size="mini" text="查看思路" @click="showAnswer(item)"></u-button>
							<!-- v-else -->	<u-button type="error"  v-else  size="mini" text="查看逐字稿" @click="showAnswer(item)"></u-button>
								</div>
								<!-- v-if="item.answerDetail" -->
						</div>
						
					</div>
				</template>
				
				<template v-else>
					<div class="item" v-for="(item,index) in timuDetail" :key="index">
						<h2>第（{{index + 1}}）套</h2>
						<div v-for="(list,j) in item" :key="j">
							
							<h3><u-parse :content="(j + 1) + '、' + list.title" :showImgMenu="false"></u-parse></h3>
							<div class="btn" v-if="userinfo.limit == '老师'">
								<u-button type="error" size="mini"  v-if="list.status=='silu'"  text="查看思路" @click="showAnswer(list)"></u-button>
							<!-- -->	<u-button type="error"  v-else size="mini"  text="查看逐字稿" @click="showAnswer(list)"></u-button>
								</div>
							<u-gap height="15" :bgColor="darkmode ? '#232031' : '#fff'"></u-gap>
						</div>
					</div>
				</template>
			</div>
		</div>
		<div class="tools">
			<div class="icon" @click="showTools = !showTools">
				<u-icon name="grid"></u-icon>
			</div>
			<div class="toolsList" :class="showTools ? 'show': ''">
				<div class="item">
					<h4>深色模式</h4>
					<div class="switchBox">
						<u-switch v-model="darkmode" @change="changedarkmode" size="20"></u-switch>
					</div>
				</div>
			</div>
		</div>
		
		<div class="footer">
			<image src="https://www.baixuemianshi.com/zhuanti/images/footer_xcx.jpg" mode="widthFix"></image>
		</div>
		<canvas canvas-id="waterMark" class="waterMark" style="width: 100vw; height: 100vh;"></canvas>
		<image :src="waterMarkUrl" class="waterMark-bg" style="width: 100vw; height: 100vh;"></image>
	</div>
</template>

<script>
	import {
		mapActions
	} from 'vuex';
	const db = wx.cloud.database()
	export default {
		data() {
			return {
				waterMarkUrl: "",
				windowWidth: 100,
				windowHeight: 100,
				zutiTitle:"",
				banjiDetail:"",
				selectIndex:[],
				timuDetail:"",
				leixings: [],
				lxList: [],
				lists: [],
				yjCurrent: 1,
				filterLxName: "全部类型",
				ejlxList: [],
				filterErlx: "",
				ejCurrent: 1,
				searchVal: "",
				searchLoading:true,
				addSuccess:false,
				
				selectTao: 0,
				
				allSelect:[],
				
				showDetail: false,
				pages:0,
				
				userinfo:"",
				darkmode:false,
				showTools: false
			}
		},
		onShareAppMessage(res) {
			if (res.from === 'button') { // 来自页面内分享按钮
				return {
					title: `题库-学员端`, //分享标题
					path: '/pages/nbhd/tiku/into', //分享页面路径
					imageUrl: 'https://www.baixuemianshi.com/zhuanti/tiku/images/xcx_share.jpg', //分享图标
					desc: '雪恒教育-白雪面试', //自定义分享描述 
				}
			} else {
				return {
					title: `题库-学员端`, //分享标题
					path: '/pages/nbhd/tiku/into', //分享页面路径
					imageUrl: 'https://www.baixuemianshi.com/zhuanti/tiku/images/xcx_share.jpg', //分享图标
					desc: '雪恒教育-白雪面试', //自定义分享描述 
				}
			}
		},
		onLoad(options) {
			this.zutiTitle = options.title
			this.getTimuInfo()
			
			let dark = uni.getStorageSync("darkmode")
			if(dark){
				this.darkmode = true
				this.setDark()
			}
		},
		onShow() {
			if(wx.setVisualEffectOnCapture){
			    wx.setVisualEffectOnCapture({
			        visualEffect: 'hidden',
			        success:(res) => {
			            console.log('success回调',res)
			        },
			    })
			}
		},
		onHide() {
			if(wx.setVisualEffectOnCapture){
			    wx.setVisualEffectOnCapture({
			        visualEffect: 'none',
			        success:(res) => {
			            console.log('success回调',res)
			        },
			    })
			}
		},
		computed: {
			userphone() {
				return this.$store.getters.userphone ? this.$store.getters.userphone : '';
			},
			selectBanji(){
				return this.$store.getters.selectBanji ? this.$store.getters.selectBanji : '';
			},
			searchList(){
				let that = this
				let l = []
				if(that.searchVal){
					l = that.lists.filter(item => item.title.indexOf(that.searchVal) != -1)
				}else{
					l = that.lists
				}
				return l
			}
			
		},
		created() {
			let that = this;
			// that.getLeixing()
		},
		mounted() {
			let that = this 
			
			wx.hideLoading()
			
			if(this.userphone){
				this.drawWaterMark('雪恒教育-白雪面试 ' + this.userphone);
			}else{
				this.drawWaterMark('雪恒教育-白雪面试');
			}
			that.getuserStatus()
		},
		watch: {},
		methods: {
			...mapActions(['LogSetPhone','LogSetBanji']),
			changedarkmode(e){
				this.darkmode = e
				uni.setStorageSync("darkmode",this.darkmode)
				this.setDark()
			},
			setDark(){
				let that = this
				if(this.darkmode){
					uni.setNavigationBarColor({
						frontColor: '#ffffff', 
						backgroundColor: '#11101c', 
						navigationBarTextStyle: '#ffefdd',
					})
				}else{
					uni.setNavigationBarColor({
						frontColor: '#000000', 
						backgroundColor: '#ffffff', 
						navigationBarTextStyle: '#ffffff',
					})
				}
				
			},
			drawWaterMark(text) {
				this.getDeviceInfo().then(response => {
					const w = response.windowWidth;
					const h = response.windowHeight;
					this.windowWidth = w;
					this.windowHeight = h;
					//创建canvas绘制上下文
					const canvas = uni.createCanvasContext("waterMark", this);
					canvas.clearRect(0, 0, w, h)
					//开始创建一个路径，需要调用fill或者stroke才会使用路径进行填充或描边。
					canvas.beginPath();
					canvas.setFontSize(18);
					canvas.setFillStyle('rgba(0, 0, 0, 0.1)');
					canvas.rotate(-30 * Math.PI / 180)
					if (text) {
						text = this.trim(text)
					}
					const maxWidth = text.length * 18
					for (let x = 0; x < 10; x++) {
						for (let y = 0; y < 20; y++) {
							//在画布上绘制被填充的文本。
							canvas.fillText(text, (maxWidth + 40) * x - 400, 90 * y + 20);
						}
					}
					//将之前在绘图上下文中的描述（路径、变形、样式）画到 canvas 中。
					canvas.draw(false, () => {
						//把当前画布指定区域的内容导出生成指定大小的图片
						uni.canvasToTempFilePath({
							canvasId: "waterMark",
							complete: (res) => {
								this.waterMarkUrl = res.tempFilePath
							}
						}, this)
					})
				});
			},
			trim(str) {
				let trimStr = '';
				const reg = /[\t\r\f\n\s]*/g;
				if (typeof str === 'string') {
					trimStr = str.replace(reg, '');
				}
				return trimStr
			},
			/**
			 * 获取当前设备的信息
			 * **/
			getDeviceInfo() {
				return new Promise((resolve, reject) => {
					uni.getSystemInfo({
						success: (sysInfo) => {
							resolve(sysInfo)
						}
					})
				})
			},
			getuserStatus(){
				let that = this
				db.collection('nbhd_tiku_students').where({
					phone: that.userphone,
					banji: that.selectBanji
				}).get({
					success: function(res){
						if(res.data.length > 0){
							that.userinfo = res.data[0]
						}
					}
				})
			},
			getTimuInfo(){
				let that = this
				db.collection('nbhd_n_tiku_tmList').where({
					banji: that.selectBanji,
					zTitle: that.zutiTitle
				}).get({
					success: function(res) {
						if(res.data.length > 0){
							that.banjiDetail = res.data[0]
							that.timuDetail = res.data[0].list
						}
					}
				})
			},
			showAnswer(item){
				let that = this
				uni.navigateTo({
					url:"./answer?tmid=" + item._id
				})
			},
		},
		components: {
			
		}
	};
</script>

<style scoped>
	@import url(./index.css);
	
</style>